// wii libraries
#include <asndlib.h>
#include <mp3player.h>

// custom libraries
#include "..\..\include\logger.h"
#include "..\..\include\audio\sound.h"
#include "..\..\include\audio\sound_renderer.h" // matching header


// ==================================
//    CONSTRUCTORS and DESTRCUTORS
// ==================================

SoundRenderer::SoundRenderer()
{
	initializeLibrary();
}

SoundRenderer::~SoundRenderer()
{
	if (isPlaying())
		stop();
    exitLibrary();
}


// ==================================
//         PUBLIC FUNCTIONS
// ==================================
	
void SoundRenderer::play(Sound* sound) 
{
    if (!isPlaying())
        MP3Player_PlayBuffer(sound->getData(), (s32)sound->getSize(), 0);
}

void SoundRenderer::stop()
{
    if (isPlaying())
         MP3Player_Stop();
}

bool SoundRenderer::isPlaying()
{
    return MP3Player_IsPlaying();
}

// ==================================
//       INITIALIZE and EXIT
// ==================================

bool SoundRenderer::initializeLibrary()
{
	Logger::Instance()->addEntry(Logger::INFO,
		"SoundRenderer: Initializing libraries...");
	
	ASND_Init();
	MP3Player_Init();
	
	Logger::Instance()->addEntry(Logger::INFO,
		"SoundRenderer: Initialization successful.");
	return true; // Cannot fail
}

bool SoundRenderer::exitLibrary()
{
	Logger::Instance()->addEntry(Logger::INFO,
		"SoundRenderer: Exiting libraries...");

	ASND_End();
	// MP3Player has no exit function.
	
	Logger::Instance()->addEntry(Logger::INFO,
		"SoundRenderer: Exit successful.");
	return true;
}
